/*
 * AuthenticationJdbcDaoImpl.java
 *
 * Created on 12 February 2007, 01:22
 */

package springweb.util.security;

import java.lang.annotation.Inherited;

import org.acegisecurity.userdetails.UserDetails;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.acegisecurity.userdetails.jdbc.JdbcDaoImpl;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

//import org.hibernate.SessionFactory;
//import org.hibernate.cfg.AnnotationConfiguration;
//import org.hibernate.classic.Session;
import org.springframework.dao.DataAccessException;

/**
 *
 * @author Andrei Tudose
 */
public class AuthenticationJdbcDaoImpl extends JdbcDaoImpl {
    
    private String[] userInfoObjectTypes;
    
  //  private static final SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();;
    Configuration cfg = new Configuration();
    SessionFactory factory = cfg.configure().buildSessionFactory();

    public UserDetails loadUserByUsername(String username) {
    	System.out.println("Inside loadUserByUsername : "+userInfoObjectTypes.length);
    	System.out.println("UserName :"+username);
    	 for (int i = 0; i < userInfoObjectTypes.length; i++) {
    		 System.out.println("Property :" +i+ " : "+userInfoObjectTypes[i]);
    	 }
    	 UserDetails user = super.loadUserByUsername(username);
    	 
    	 Session session = factory.openSession();
    	    Transaction transaction = session.beginTransaction();
    	    
    	    Query query = session.createQuery("SELECT USERNAME, PASSWORD, USERROLE FROM USERDB WHERE USERNAME = '" + username + "' ");
    	    
    	    System.out.println("Query : "+query);
    	    System.out.println("uniqueResult : "+(String)query.uniqueResult());
    	    
        /*try {
          
            UserDetails user = super.loadUserByUsername(username);
            Session session = sessionFactory.openSession();                
            for (int i = 0; i < userInfoObjectTypes.length; i++) {
            	System.out.println("userInfoObjectTypes : "+userInfoObjectTypes[i]);
                Object userInfo = session.createQuery("from " + userInfoObjectTypes[i] + " where username = '" + username + "'").uniqueResult();
                if(userInfo != null)
                    return new CustomUser(user.getUsername(), user.getPassword(), user.isEnabled(), user.getAuthorities(), userInfo);
            }
            return new CustomUser(user.getUsername(), user.getPassword(), user.isEnabled(), user.getAuthorities());
            
        } catch (UsernameNotFoundException ex1) {
            ex1.printStackTrace();
            throw ex1;
        } catch (DataAccessException ex2) {
            ex2.printStackTrace();
            throw ex2;
        }*/
    	 return user;
    }

    public void setUserInfoObjectTypes(String[] userInfoObjectTypes) {
        this.userInfoObjectTypes = userInfoObjectTypes;
    }
}
